<?php
/**
 * boatModel.php
 * This file holds the rules and settings for all members
 *
 * @author Peter, Thomas, Tryggve
 * @version 0.1
 * @copyright Oh, yes!
 * @package default
 */

 // This is dependent on memberClass...
require_once("models/memberClass.php") ;
require_once("models/baseModel.php") ;

/**
 * @package default
 * @subpackage classes
 */
class MemberModel extends BaseModel {
    
    // declare a variable to hold the boatModel-class
    protected $m_boatModel ;

    // declare some constants for table and class names
    const TABLE_NAME = 'members' ;
    const TABLE_ID = 'memberId' ;
    const CLASS_NAME = 'MemberClass' ;

    /**
     * __construct
     * 
     * @param DbModel $a_db
     * @param BoatModel $a_boatModel
     * 
     * @returns NULL
     */
    public function __construct(DbModel $a_db, BoatModel $a_boatModel) {
        parent::__construct($a_db) ;
        $this->m_boatModel = $a_boatModel ;
    }
    
    /**
     * create
     */
    public function create($a_memberInfo) {
        
		$a_memberInfo['ssn'] = ValidationModel::trimSSN($a_memberInfo['ssn']);
		
        if(ValidationModel::validateMemberInfo($a_memberInfo) AND parent::create($a_memberInfo)) {
            $this::addMessage("Medlemmen har blivit skapad!") ;
        } else {
            $this::addMessage("Medlemmen kunde inte skapas!") ;
        }
        
    }

    public function read($a_id = null, $a_tableId = null) {
        
        $members = parent::read($a_id, $a_tableId) ;
        
        // get the boats for each member
        foreach ($members as $member) {
            $member->addBoats($this->m_boatModel->read($member->memberId, 'memberId')) ;
        }
        
        return $members ;
    }
    
    public function update($a_memberInfo) {
        
        if(ValidationModel::validateMemberInfo($a_memberInfo) AND parent::update($a_memberInfo)) {
            $this::addMessage("Medlemmen har blivit ändrad!") ;
        } else {
            $this::addMessage("Medlemmen kunde inte ändras!") ;
        }
        
    }

    /**
     * delete
     * 
     * @param int $a_id
     * 
     * @return boolean
     */
    public function delete($a_id) {
        
         if(parent::delete($a_id)) {
            // and all his/her boats
            $this->m_db->delete("WHERE memberId = $a_id", 'boats') ;    
            $this::addMessage("Medlemmen och alla dess båtar har blivit borttagna!") ;
        } else {
            $this::addMessage("Medlemmen kunde inte tas bort!") ;
        }

    }


}
